home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Source.bin
/
SimpleWizardController.java
< prev
next >
Wrap
Text File
|
1998-10-21
|
17KB
|
610 lines
/*
* SimpleWizardController.java
*/
package symantec.itools.awt;
import java.awt.Component;
import java.util.Vector;
/**
* SimpleWizardController implements the WizardController interface.
* It is the default WizardController used by Wizard and provides
* a simple behaviour that can be easily overriden. It manages a stack
* containing the history of pages shown, and provides consistent
* defaults for the navigation buttons.
*/
public class SimpleWizardController implements WizardController
{
/**
* Create a SimpleWizardController.
*
* @param wizard
* A WizardInterface to talk to.
*/
public SimpleWizardController(WizardInterface wizard)
{
//debug("SimpleWizardController");
this.wizard = wizard;
}
/**
* Called before the first page is shown.
* Default behavior: clean-up the history stack.
*/
public void doPrepare()
{
stack.removeAllElements();
}
/**
* Called before the first page is shown in the special case of
* this SimpleWizardController.
* Default behavior: clean-up the history stack and stack the first
* pages.
*/
public void doPrepare(int index)
{
doPrepare();
for (int i = 0; i < index; i++)
stack.addElement(wizard.getComponentAt(i));
}
/**
* Called before a page is shown.
* Default behavior: add the page to the history stack if the action is
* NEXT, remove the last page from the history stack if the action is
* PREVIOUS.
*
* @param comp
* The page that will be shown.
* @param action
* The action that has led to this page, either PREVIOUS or NEXT. If it is
* the first page of the wizard, the action is NEXT.
* @see #getPreviousPage
*/
public void preparePage(Component comp, int action)
{
//debug("preparePage");
// Don't maintain a stack at design time
if (!getIgnoreDesignTime() && java.beans.Beans.isDesignTime())
return;
if (action == NEXT)
{
stack.addElement(comp);
//System.err.println("Added element, comp = " + comp.hashCode());
}
else if (action == PREVIOUS)
{
if (stack.size() > 0) {
//System.err.println("preparePage removeElementAt");
stack.removeElementAt(stack.size() - 1);
//System.err.println("Removed element, comp = " + comp.hashCode());
}
}
}
/**
* Called after a page is shown.
* Default behavior: none.
*
* @param comp
* The page that has been shown.
*/
public void pageShown(Component comp)
{
//debug("pageShown");
// do nothing
}
/**
* Try to validate a page.
* Default behavior: return the state of the button associated with
* the action, using the isXxxEnabled methods. For example, if the
* controller has the Previous button enabled, this method will return
* true, meaning that it is valid to go back.
* If the result is true, then the Wizard will proceed with the
* action, else the state will not change.
*
* @param comp
* The page that must be validated.
* @param target
* The page that will be shown if the action is PREVIOUS or NEXT, null
* otherwise.
* @param action
* The action that must be validated, PREVIOUS, NEXT, FINISH, CANCEL or HELP.
* @return
* True if the action is validated, false otherwise.
*/
public boolean validatePage(Component comp, Component target, int action)
{
//debug("validatePage");
switch(action)
{
case NEXT:
return isNextEnabled();
case PREVIOUS:
return isPreviousEnabled();
case FINISH:
return isFinishEnabled();
case CANCEL:
return isCancelEnabled();
case HELP:
return isHelpEnabled();
default:
return false;
}
}
/**
* Called before the page is hidden.
* Default behavior: none.
*
* @param comp
* The page that has been shown.
*/
public void pageHidden(Component comp)
{
//debug("pageHidden");
// do nothing
}
/**
* Called when the FINISH action has been validated.
* Default behavior: none.
*/
public void doFinish()
{
//debug("doFinish");
// do nothing
}
/**
* Called when the CANCEL action has been validated.
* Default behavior: none.
*/
public void doCancel()
{
//debug("doCancel");
// do nothing
}
/**
* Called when the HELP action has been validated.
* Default behavior: none.
*/
public void doHelp()
{
//debug("doHelp");
// do nothing
}
/**
* Tells if the Previous button must be enabled.
* Default behavior: if setPreviousEnabled has been called,
* return that value. Else return true if the getPreviousPage method
* returns a page, false otherwise.
*
* @return
* True if the Previous button must be enabled, false otherwise.
*/
public boolean isPreviousEnabled()
{
if (previousEnabled == 0)
return false;
else if (previousEnabled == 1)
return true;
return (getPreviousPage() != null);
}
/**
* Tells if the Next button must be enabled.
* Default behavior: if setNextEnabled has been called,
* return that value. Else return true if the getNextPage method
* returns a page, false otherwise.
*
* @return
* True if the Next button must be enabled, false otherwise.
*/
public boolean isNextEnabled()
{
if (nextEnabled == 0)
return false;
else if (nextEnabled == 1)
return true;
return (getNextPage() != null);
}
/**
* Tells if the Finish button must be enabled.
* Default behavior: if setFinishEnabled has been called,
* return that value. Else return true if the current page
* is the last page in the Wizard container.
*
* @return
* True if the Finish button must be enabled, false otherwise.
*/
public boolean isFinishEnabled()
{
if (finishEnabled == 0)
return false;
else if (finishEnabled == 1)
return true;
int current = wizard.getSelectedIndex();
int count = wizard.getPageCount();
if (getIgnoreDesignTime() || !java.beans.Beans.isDesignTime())
{
if ((count > 0) && (current != -1))
return (current == (count - 1));
else
return false;
}
else
{
return false;
}
}
/**
* Tells if the Cancel button must be enabled.
* Default behavior: if setCancelEnabled has been called,
* return that value. Else return true.
*
* @return
* True if the Cancel button must be enabled, false otherwise.
*/
public boolean isCancelEnabled()
{
if (cancelEnabled == 0)
return false;
else if (cancelEnabled == 1)
return true;
if (!getIgnoreDesignTime() && java.beans.Beans.isDesignTime())
return false;
else
return true;
}
/**
* Tells if the Help button must be enabled.
* Default behavior: if setHelpEnabled has been called,
* return that value. Else return true.
*
* @return
* True if the Help button must be enabled, false otherwise.
*/
public boolean isHelpEnabled()
{
if (helpEnabled == 0)
return false;
else if (helpEnabled == 1)
return true;
if (!getIgnoreDesignTime() && java.beans.Beans.isDesignTime())
return false;
else
return true;
}
/**
* Return the previous page.
* Default behavior: if a previous page has been set with
* setPreviousPage, return this page. If a previous page index has
* been set with setPreviousPageIndex, return the associated page.
* Else, return the next to last element on the stack. If there is none,
* return null.
* This method is called before and after a validatePage, and by the
* default implementation of isPreviousEnabled.
*
* @return
* The previous page to show, null if there is none.
* @see #setPreviousPage
* @see #setPreviousPageIndex
* @see #preparePage
*/
public Component getPreviousPage()
{
if (getIgnoreDesignTime() || !java.beans.Beans.isDesignTime())
{
if (previousPage != null)
return previousPage;
else if (previousPageIndex != -1)
return wizard.getComponentAt(previousPageIndex);
else if (stack.size() > 1)
return (Component)stack.elementAt(stack.size() - 2);
else
return null;
}
else
{
int current = wizard.getSelectedIndex();
int count = wizard.getPageCount();
if ((count > 0) && (current > 0))
return wizard.getComponentAt(current - 1);
else
return null;
}
}
/**
* Return the next page.
* Default behavior: if a next page has been set with
* setNextPage, return this page. If a next page index has
* been set with setNextPageIndex, return the associated page.
* Else, return the next page in the Wizard components order,
* page 0 if no page has been shown.
* If there is none, return null.
* This method is called before and after a validatePage, and by the
* default implementation of isNextEnabled.
*
* @return
* The next page to show, null if there is none.
* @see #setNextPage
* @see #setNextPageIndex
*/
public Component getNextPage()
{
int current = wizard.getSelectedIndex();
int count = wizard.getPageCount();
if (getIgnoreDesignTime() || !java.beans.Beans.isDesignTime())
{
if (nextPage != null)
return nextPage;
else if (nextPageIndex != -1)
return wizard.getComponentAt(nextPageIndex);
else if ((count > 0) && (current != -1) && (current != (count - 1)))
return wizard.getComponentAt(current + 1);
else if ((count > 0) && (current == -1))
return wizard.getComponentAt(0);
else
return null;
}
else
{
if ((count > 0) && (current != -1) && (current != (count - 1)))
return wizard.getComponentAt(current + 1);
else
return null;
}
}
// Explicit chain information provided by interactions
/**
* Set an index for the previous page.
* This index is used by the default implementation of getPreviousPage.
* It is reset before a new page is prepared.
*
* @see #getPreviousPage
* @see #resetChainInfo
*/
public void setPreviousPageIndex(int index)
{
previousPageIndex = index;
}
/**
* Set an index for the next page.
* This index is used by the default implementation of getNextPage.
* It is reset before a new page is prepared.
*
* @see #getNextPage
* @see #resetChainInfo
*/
public void setNextPageIndex(int index)
{
nextPageIndex = index;
}
/**
* Set a previous page.
* This page is used by the default implementation of getPreviousPage.
* It is reset before a new page is prepared.
*
* @see #getPreviousPage
* @see #resetChainInfo
*/
public void setPreviousPage(Component comp)
{
previousPage = comp;
}
/**
* Set a next page.
* This page is used by the default implementation of getNextPage.
* It is reset before a new page is prepared.
*
* @see #getNextPage
* @see #resetChainInfo
*/
public void setNextPage(Component comp)
{
nextPage = comp;
}
/**
* Set the state of the Previous button.
* This state is used by the default implementation of isPreviousEnabled.
* It is reset before a new page is prepared.
*
* @see #isPreviousEnabled
* @see #resetChainInfo
*/
public void setPreviousEnabled(boolean status)
{
previousEnabled = status ? 1 : 0;
wizard.updateButtonsState();
}
/**
* Set the state of the Next button.
* This state is used by the default implementation of isNextEnabled.
* It is reset before a new page is prepared.
*
* @see #isNextEnabled
* @see #resetChainInfo
*/
public void setNextEnabled(boolean status)
{
nextEnabled = status ? 1 : 0;
wizard.updateButtonsState();
}
/**
* Set the state of the Finish button.
* This state is used by the default implementation of isFinishEnabled.
* It is reset before a new page is prepared.
*
* @see #isFinishEnabled
* @see #resetChainInfo
*/
public void setFinishEnabled(boolean status)
{
finishEnabled = status ? 1 : 0;
wizard.updateButtonsState();
}
/**
* Set the state of the Cancel button.
* This state is used by the default implementation of isCancelEnabled.
* It is reset before a new page is prepared.
*
* @see #isCancelEnabled
* @see #resetChainInfo
*/
public void setCancelEnabled(boolean status)
{
cancelEnabled = status ? 1 : 0;
wizard.updateButtonsState();
}
/**
* Set the state of the Help button.
* This state is used by the default implementation of isHelpEnabled.
* It is reset before a new page is prepared.
*
* @see #isHelpEnabled
* @see #resetChainInfo
*/
public void setHelpEnabled(boolean status)
{
helpEnabled = status ? 1 : 0;
wizard.updateButtonsState();
}
/**
* Reset the chain information set by setPreviousPageIndex,
* setNextPageIndex, setPreviousPage, setNextPage, and
* setXxxEnabled. This is called before a new page is preared.
*
* @see #setPreviousPageIndex
* @see #setNextPageIndex
* @see #setPreviousPage
* @see #setNextPage
*/
public void resetChainInfo()
{
previousPage = null;
previousPageIndex = -1;
nextPage = null;
nextPageIndex = -1;
previousEnabled = -1;
nextEnabled = -1;
finishEnabled = -1;
cancelEnabled = -1;
helpEnabled = -1;
}
// Member variables
/**
* The WizardInterface.
*/
protected WizardInterface wizard;
/**
* The stack containing the history of the pages shown.
*
* @see #preparePage
* @see #getPreviousPage
*/
protected Vector stack = new Vector();
/**
* The index of the previous ("back") page, or -1 if none.
* @see #setPreviousPageIndex
*/
protected int previousPageIndex = -1;
/**
* The index of the next page, or -1 if none.
* @see #setNextPageIndex
*/
protected int nextPageIndex = -1;
/**
* The previous ("back") page, or <code>null</code> if none.
* @see #setPreviousPage
*/
protected Component previousPage;
/**
* The next page, or <code>null</code> if none.
* @see #setNextPage
*/
protected Component nextPage;
/**
* True if the Previous button is enabled.
*/
protected int previousEnabled = -1;
/**
* True if the Next button is enabled.
*/
protected int nextEnabled = -1;
/**
* True if the Finish button is enabled.
*/
protected int finishEnabled = -1;
/**
* True if the Cancel button is enabled.
*/
protected int cancelEnabled = -1;
/**
* True if the Help button is enabled.
*/
protected int helpEnabled = -1;
/**
* Tell the wizard to ignore the value of java.beans.Beans.isDesignTime().
*/
public void setIgnoreDesignTime(boolean ignore)
{
ignoreDesignTime = ignore;
}
public boolean getIgnoreDesignTime()
{
return ignoreDesignTime;
}
private boolean ignoreDesignTime = false;
// Debug
/*
private void debug(String s) {
System.err.println("SimpleWizardController: " + s);
}
*/
}